package com.example.allshengmedical.experimentrun.activity;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import com.example.allshengmedical.util.FileManagerUtil;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

/**
 * @author boye009 扩增数据库
 * 
 *         暂没用到
 */
public class RawDataTwoDBHelper extends SQLiteOpenHelper {
	final static String RawDataDBName = "RawDataDB.db";
	final static String RawDataTableName = "RawDataDB";
	final static String ChannelIndex = "ChannelIndex";
	final static String LightNum = "LightNum";
	final static String Line = "Line";
	final static String PointIndex = "PointIndex";
	public static final int VERSION = 1;

	// public static final String TABLENUMBER = "1";
	SQLiteDatabase database;
	File file;

	public RawDataTwoDBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	public RawDataTwoDBHelper(Context context) {
		// TODO Auto-generated constructor stub
		super(context, RawDataDBName, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		file = createDBFile();
		database = SQLiteDatabase.openOrCreateDatabase(file, null);
		this.database = db;
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append("CREATE  TABLE  " + RawDataTableName + "  ( ");
		sqlBuilder
				.append("_id Integer primary key , ChannelIndex TEXT , LightNum TEXT , Line TEXT , PointIndex TEXT )");
		database.execSQL(sqlBuilder.toString());
	}

	public SQLiteDatabase sqlDB() {
		File file = createDBFile();
		SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(file,
				null);
		return database;
	}

	/**
	 * @param channelIndex
	 *            通道索引
	 * @param lightNum
	 *            光强值
	 * @param line
	 *            第几条
	 * @param pointIndex
	 *            第几次
	 */
	public void add(String channelIndex, String lightNum, String line,
			String pointIndex) {
		file = createDBFile();
		database = SQLiteDatabase.openOrCreateDatabase(file, null);
		ContentValues contentValues = new ContentValues();
		contentValues.put(ChannelIndex, channelIndex);
		contentValues.put(LightNum, lightNum);
		contentValues.put(Line, line);
		contentValues.put(PointIndex, pointIndex);
		database.insert(RawDataTableName, null, contentValues);
		// database.close();
	}

	public void close() {
		if (database != null)
			database.close();
	}

	public void deleteDBFile() {
		File file = new File(Environment.getExternalStorageDirectory() + "/"
				+ "AllShengDB" + "/" + RawDataDBName);
		if (file.exists()) {
			file.delete();
		}
	}

	public File createDBFile() {
		FileManagerUtil fileManagerUtil = new FileManagerUtil();
		fileManagerUtil.createDB();
		File file = new File(Environment.getExternalStorageDirectory() + "/"
				+ "AllShengDB" + "/" + RawDataDBName);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return file;

	}

	public void delete() {
		try {
			deleteDBFile();
			file = createDBFile();
			database = SQLiteDatabase.openOrCreateDatabase(file, null);
			onCreate(database);
			if (database != null) {
				database.execSQL("DELETE FROM " + RawDataTableName);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public ArrayList<String> queryChannel(SQLiteDatabase db) {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = db.rawQuery("SELECT ChannelIndex FROM "
				+ RawDataTableName, null);
		while (cursor.moveToNext()) {
			String string = cursor.getString(cursor
					.getColumnIndex("ChannelIndex"));
			arrayList.add(string);
		}
		// tempArrayList = dbHelper.query();
		// database.close();
		return arrayList;

	}

	public ArrayList<String> queryWhereChannel(SQLiteDatabase db, int t) {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = db.rawQuery("SELECT LightNum FROM " + RawDataTableName
				+ " where ChannelIndex = " + t, null);
		while (cursor.moveToNext()) {
			String string = cursor.getString(cursor.getColumnIndex("LightNum"));
			arrayList.add(string);
		}
		// tempArrayList = dbHelper.query();
		// database.close();
		return arrayList;

	}

	public ArrayList<String> queryLight(SQLiteDatabase db) {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = db.rawQuery("SELECT LightNum FROM " + RawDataTableName,
				null);
		while (cursor.moveToNext()) {
			String string = cursor.getString(cursor.getColumnIndex("LightNum"));
			arrayList.add(string);
		}
		// tempArrayList = dbHelper.query();
		// database.close();
		return arrayList;

	}

	public ArrayList<String> queryLine(SQLiteDatabase db, int line) {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = db.rawQuery("SELECT LightNum FROM " + RawDataTableName
				+ " where Line = " + line, null);
		while (cursor.moveToNext()) {
			String string = cursor.getString(cursor.getColumnIndex("LightNum"));
			arrayList.add(string);
		}
		// tempArrayList = dbHelper.query();
		// database.close();
		return arrayList;

	}

	/**
	 * 查询第几条线第几通道
	 * 
	 * @param db
	 * @param line
	 * @param channelIndex
	 * @return
	 */
	public ArrayList<String> queryLine(SQLiteDatabase db, int line,
			int channelIndex) {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = db.rawQuery("SELECT LightNum FROM " + RawDataTableName
				+ " where Line = " + (line) + " and ChannelIndex="
				+ channelIndex, null);
		while (cursor.moveToNext()) {
			String string = cursor.getString(cursor.getColumnIndex("LightNum"));
			arrayList.add(string);
		}
		// tempArrayList = dbHelper.query();
		try {
			db.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}

		db.close();
		cursor.close();
		return arrayList;

	}

	/**
	 * 查询第几条线第几通道第几个点
	 * 
	 * @param db
	 * @param line
	 * @param channelIndex
	 * @param PointIndex
	 * @return
	 */
	public String queryLine(SQLiteDatabase db, int line, int channelIndex,
			int PointIndex) {
		String arrayList = null;
		Cursor cursor = db.rawQuery("SELECT LightNum FROM " + RawDataTableName
				+ " where Line = " + (line) + " and ChannelIndex="
				+ channelIndex + " and PointIndex=" + PointIndex, null);
		while (cursor.moveToNext()) {
			arrayList = cursor.getString(cursor.getColumnIndex("LightNum"));
		}
		// tempArrayList = dbHelper.query();
		try {
			db.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}

		db.close();
		cursor.close();
		return arrayList;

	}

	public ArrayList<String> queryLine(SQLiteDatabase db) {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = db.rawQuery("SELECT Line FROM " + RawDataTableName,
				null);
		while (cursor.moveToNext()) {
			String string = cursor.getString(cursor.getColumnIndex("Line"));
			arrayList.add(string);
		}
		// tempArrayList = dbHelper.query();
		// database.close();
		return arrayList;

	}

	public ArrayList<String> queryPointIndex(SQLiteDatabase db) {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = db.rawQuery("SELECT PointIndex FROM "
				+ RawDataTableName, null);
		while (cursor.moveToNext()) {
			String string = cursor.getString(cursor
					.getColumnIndex("PointIndex"));
			arrayList.add(string);
		}
		// tempArrayList = dbHelper.query();
		// database.close();
		return arrayList;

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

}
